package main

import (
	"fmt"
)

func nextPermutation(nums []int) {
	for i := len(nums) - 1; i > 0; i-- {
		if nums[i] > nums[i-1] {
			for j := len(nums) - 1; j > 0; j-- {
				if nums[j] > nums[i-1] {
					nums[i-1], nums[j] = nums[j], nums[i-1]
					//reversePartial(nums, i)
					reverse2(nums[i:])
					return
				}
			}
		}
	}
	reverse2(nums)
}

// 反转数组
func reverse2(nums []int) {
	for i, j := 0, len(nums)-1; i < j; i, j = i+1, j-1 {
		nums[i], nums[j] = nums[j], nums[i]
	}
}

func main() {
	nums1 := []int{6, 5, 1, 2, 4, 3}
	nextPermutation(nums1)
	fmt.Println("asd:   ", nums1)
}
